package com.example.school.controller;

import com.example.school.entity.Course;
import com.example.school.entity.Student;
import com.example.school.entity.XuanKe;
import com.example.school.service.CourseService;
import com.example.school.service.StudentService;
import com.example.school.service.XuanKeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private   CourseService courseService;

    @Autowired
    private  XuanKeService xuanKeService;


//    @RequestMapping(value = "/login", method = RequestMethod.POST)
//    @ResponseBody
//    public Map<String, Object> login(@RequestBody Student student, HttpServletRequest request) {
//        Map<String, Object> retData = new HashMap<>();
//        HttpSession session = request.getSession();
//
//        // 进行登录验证
//        boolean isSuccess = studentService.login(student.getUsername(), student.getPassword());
//        if (isSuccess) {
//            session.setAttribute("userName", student.getUsername());
//            session.setAttribute("passWord", student.getPassword());
//            session.setAttribute("role", student.getRole()); // 存储角色信息
//            retData.put("code", 200);
//            retData.put("message", "登录成功");
//        } else {
//            retData.put("code", 401); // 使用401状态码表示未授权
//            retData.put("message", "登录失败，用户名或密码错误");
//        }
//        return retData;
//    }



    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Student student, HttpServletRequest request) {
        Map<String, Object> retData = new HashMap<>();
        HttpSession session = request.getSession();

        // 进行登录验证
        boolean isSuccess = studentService.login(student.getUsername(), student.getPassword());
        if (isSuccess) {
            session.setAttribute("userName", student.getUsername());
            session.setAttribute("role", "student");
            retData.put("code", 200);
            retData.put("message", "登录成功");
        } else {
            retData.put("code", 401); // 使用401状态码表示未授权
            retData.put("message", "登录失败，用户名或密码错误");
        }
        return retData;
    }

    @PutMapping("/changePassword")
    public ResponseEntity<String> changePassword(@RequestParam String username,
                                                 @RequestParam String oldPassword,
                                                 @RequestParam String newPassword) {
        boolean success = studentService.changePassword(username, oldPassword, newPassword);
        if (success) {
            return ResponseEntity.ok("密码更改成功!");
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("旧密码错误!");
        }
    }



    @ResponseBody
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Map<String, Object> searchStudents(@RequestBody Map<String, Object> params) {
        return studentService.searchStudents(params);
    }



    @Autowired
    public void CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @GetMapping("/courses/teacher")
    public List<Course> findCoursesByTeacherName(@RequestParam String name) {
        return courseService.findCoursesByTeacherName(name);
    }

    @GetMapping("/courses/college")
    public List<Course> findCoursesByCollege(@RequestParam String college) {
        return courseService.findCoursesByCollege(college);
    }


    @GetMapping("/score")
    @ResponseBody
    public Map<String, Object> getByUsersId(@RequestParam("username") String username) {
        Map<String, Object> retData = new HashMap<>();
        if (!username.isEmpty()) {
            Student studentNumber = studentService.getIdByUsername(username);
            retData.put("code", 200);
            retData.put("users", xuanKeService.getByUsersId(studentNumber));
        } else {
            retData.put("code", 500);
            retData.put("message", "未找到该用户");
        }
        return retData;
    }



}
